/**
 * @Author wangyl
 * @E-mail wangyl0629@foxmail.com
 **/
package com.wyl.xue.admin.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyl.xue.admin.system.mybatis.entity.SystemMenu;
import com.wyl.xue.admin.system.service.Menu;
import com.wyl.xue.admin.system.vo.MenuInfoVo;
import com.wyl.xue.admin.system.vo.MenuTree;
import com.wyl.xue.core.util.result.WebResponse;
import com.wyl.xue.core.util.result.WebResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName: MenuController
 * @Function: 菜单接口
 * @Date: 2019/12/23 22:13
 * @author wyl
 * @version V1.0
 */
@RestController
@RequestMapping("/admin/v1/menu")
@Api(tags = {"菜单信息接口"})
@AllArgsConstructor
@CrossOrigin
public class MenuController {

    private final Menu menu;

    @PostMapping(value = "/")
    @ApiOperation(value = "新增菜单信息", notes = "根据菜单信息新增菜单")
    @PreAuthorize("hasAuthority('sys:menu:add')")
    public WebResult<Boolean> addMenu(@Valid @RequestBody MenuInfoVo menuInfoVo) {
        menu.addMenu(menuInfoVo);
        return WebResponse.WebResponse.ok();
    }

    @PutMapping(value = "/{id}")
    @ApiOperation(value = "更新菜单信息", notes = "根据菜单信息更新菜单")
    @PreAuthorize("hasAuthority('sys:menu:update')")
    public WebResult<Boolean> changeMenu(@RequestBody SystemMenu systemMenu) {
        menu.updateMenu(systemMenu);
        return WebResponse.WebResponse.ok();
    }

    @DeleteMapping(value = "/{id}")
    @ApiOperation(value = "删除菜单信息", notes = "根据菜单id删除指定菜单")
    @PreAuthorize("hasAuthority('sys:menu:del')")
    public WebResult<Boolean> deleteMenu(@PathVariable Long id) {
        menu.deleteMenu(id);
        return WebResponse.WebResponse.ok();
    }

    @GetMapping(value = "/{id}/{page}/{size}")
    @ApiOperation(value = "获取指定菜单下的所有菜单")
    @PreAuthorize("hasAuthority('sys:menu:get')")
    public WebResult<Page<SystemMenu>> getMenusById(@PathVariable Long id, @PathVariable Integer page, @PathVariable Integer size) {
        return WebResponse.WebResponse.ok(menu.getSubmenuByParentIdForPage(id, page, size));
    }

    @GetMapping(value = "/tree")
    @ApiOperation(value = "获取菜单树 不包含按钮")
    @PreAuthorize("hasAuthority('sys:menu:get')")
    public WebResult<List<MenuTree>> menuTree() {
        return WebResponse.WebResponse.ok(menu.getMenuTreeExcludeFunctionMenu(-1L));
    }


    @PostMapping(value = "/search")
    @ApiOperation(value = "获取菜单树 不包含按钮")
    @PreAuthorize("hasAuthority('sys:menu:search')")
    public WebResult<List<SystemMenu>> search(@RequestBody MenuInfoVo menuInfoVo) {
        return WebResponse.WebResponse.ok(menu.searchMenu(menuInfoVo));
    }

}
